//
//
//
//
// Socket.h
//

#ifndef _SOCKET_H_
#define _SOCKET_H_

#include "common.h"
#include "datatypes.h"

#include "NetAddr.h"
#include "SocketHdl.h"
#include <sys/socket.h>

class TimeVal;
class Socket
{
	public:
		enum SHUTDOWN_OPT
		{
			_SHUT_RD = SHUT_RD,
			_SHUT_WR = SHUT_WR,
			_SHUT_RDWR = SHUT_RDWR,
		};


		enum EVENTS_OPT
		{
			_NONE_EVENT			= 0x00,
			_READ_EVENT 		= 0x01,
			_WRITE_EVENT 		= 0x02,
			_EXCEPTION_EVENT 	= 0x04,
			_TIMEOUT_EVENT 		= 0x08,
			_ALL_EVENT			= 0x0f,
		};

	public:
		Socket();
		Socket(S32 fileID);
		Socket(const Socket& other);
		Socket& operator=(const Socket& other);
		bool operator==(const Socket& other);
		virtual ~Socket();
		
		virtual void Connect(const NetAddr& addr, const TimeVal& inv) = 0;
		S32 FileID(void) const;

		SocketHdl GetSockHdl(void);
		void Write(const char* buf, U32 size);
		U32 Read(void* buf, U32 size);
		void Close();
		void Shutdown(Socket::SHUTDOWN_OPT opt);
	protected:
		SocketHdl _sockHdl;
		// only meanful when the Connect method is called successfully
		// firstly it will be invalid
		NetAddr _peerAddr;
};

#endif //_SOCKET_H_
